<?php

namespace App\Repositories;

use App\Interfaces\SearchKeywordsService;
use App\Models\Category;
use App\Models\SearchKeywords;
use DB;
use Illuminate\Support\Collection;

class SearchKeywordsRepository implements SearchKeywordsService {

    public function getFitterGoodsPaginate($keyword='',$cat_id='',$brand_id='',$stock=0,$pageSize=15,$orderType=''){


        $searchQuery = SearchKeywords::select('*');
        //关键词
        if ($keyword!=''){

            $searchQuery = $searchQuery->where('keyword','like','%'.$keyword.'%');
            //按照匹配度排序
            $searchQuery->orderByRaw(DB::raw("REPLACE(keyword,'{$keyword}','')"));
            //联合查询商品表
            $searchQuery->join('goods','search_keywords.goods_id','=','goods.goods_id');
        }else{
            $searchQuery = $searchQuery->rightJoin('goods','search_keywords.goods_id','=','goods.goods_id');
            $searchQuery = $searchQuery->whereNull('deleted_at');
        }

        $searchQuery = $searchQuery->where('goods.is_on_sale',1);

        //筛选分类

        if (intval($cat_id==0)){
            $ids = Category::getRootIds();

        }else{
            $category = Category::find($cat_id);
            $ids = new Collection();
            if ($category){
                $ids = $category->getChildIds();
            }
            $ids->push(intval($cat_id));
        }

        $searchQuery = $searchQuery->whereIn('goods.cat_id',$ids);

        //dd($searchQuery->toSql());

        //筛选品牌
        if ($brand_id!=''){

            $searchQuery = $searchQuery->where('goods.brand_id',$brand_id);
        }

        //筛选是否有货
        if ($stock==1){
            $searchQuery = $searchQuery->where('goods.goods_number','>',0);
        }

        $searchQuery = $searchQuery->groupBy('goods.goods_id');

        //销量排序 类型: sale_desc|sale_asc
        if ($orderType=='sale_desc'){
            $searchQuery = $searchQuery->orderBy('sales_num','DESC');
        }elseif($orderType=='sale_asc'){
            $searchQuery = $searchQuery->orderBy('sales_num','ASC');
        }

        //价格排序 类型:price_desc|price_asc
        if ($orderType=='price_desc'){
            $searchQuery = $searchQuery->orderBy('shop_price','DESC');
        }elseif($orderType=='price_asc'){
            $searchQuery = $searchQuery->orderBy('shop_price','ASC');
        }

        //默认 时间倒序
        $searchQuery = $searchQuery->orderBy('add_time','desc');


        $searchResult = $searchQuery->paginate($pageSize);


        return $searchResult;
    }

    /**获取相似关键词
     * @param $keyword
     * @param $num
     */
    public function getRelateKeywords($keyword,$num=10){

        $keywordQuery = SearchKeywords::where('keyword','like','%'.$keyword.'%');
        $keywordQuery = $keywordQuery->orderByRaw(DB::raw("REPLACE(keyword,'{$keyword}','')"));
        $keywordResult = $keywordQuery->limit($num)->get();

        return  $keywordResult;
    }

    /** 获取热搜关键词
     * @param int $num
     * @return mixed
     */
    public function getHotKeywords($num=10){

        return SearchKeywords::orderBy('search_numbers','desc')->groupBy('keyword')->limit($num)->get();

    }

}